﻿using System;
using System.Collections;
using System.Web;

namespace Guan.CMS.Core.Cache
{
    /// <summary>
    /// 使用Http
    /// </summary>
    public static class HttpCacheExtension
    {
        public static System.Web.Caching.Cache Cache = HttpRuntime.Cache;

        /// <summary>
        /// 添加或更新缓存
        /// </summary>
        /// <param name="key">关键字</param>
        /// <param name="o">待添加或更新对象</param>
        /// <returns></returns>
        public static string Set(string key, object o)
        {
            try
            {
                Cache.Insert(key, o);
                return null;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        /// <summary>
        /// 添加或更新缓存
        /// </summary>
        /// <param name="key">关键字</param>
        /// <param name="o">待添加或更新对象</param>
        /// <param name="absoluteExpiration">过期时间，请使用DateTime.UtcNow。如果使用绝对到期，则 slidingExpiration 参数必须为 System.Web.Caching.Cache.NoSlidingExpiration。</param>
        /// <param name="slidingExpiration">最后一次访问所插入对象时与该对象到期时之间的时间间隔。如果使用可调到期，则absoluteExpiration 参数必须为 System.Web.Caching.Cache.NoAbsoluteExpiration。</param>
        /// <returns></returns>
        public static string Set(string key, object o, DateTime absoluteExpiration, TimeSpan slidingExpiration)
        {
            try
            {
                Cache.Insert(key, o, null, absoluteExpiration, slidingExpiration);
                return null;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key">关键字</param>
        /// <returns></returns>
        public static object Get(string key)
        {
            return Cache[key];
        }

        /// <summary>
        /// 删除缓存
        /// </summary>
        /// <param name="key">关键字</param>
        /// <returns>错误信息，成功则返回null</returns>
        public static string Remove(string key)
        {
            System.Web.Caching.Cache objCache = HttpRuntime.Cache;
            try
            {
                objCache.Remove(key);
            }
            catch (Exception e)
            {
                return e.ToString();
            }

            return null;
        }

        /// <summary>
        /// 清空缓存
        /// </summary>
        /// <returns></returns>
        public static string ClearAll()
        {
            try
            {
                System.Web.Caching.Cache cache = HttpRuntime.Cache;
                var al = GetAll();
                if (al.Count > 0)
                {
                    foreach (string key in al)
                    {
                        cache.Remove(key);
                    }
                }
                return null;
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }

        /// <summary>
        /// 获取所有缓存
        /// </summary>
        /// <returns></returns>
        public static ArrayList GetAll()
        {
            var al = new ArrayList();
            System.Web.Caching.Cache cache = HttpRuntime.Cache;
            if (cache.Count > 0)
            {
                IDictionaryEnumerator cacheEnumerator = cache.GetEnumerator();
                while (cacheEnumerator.MoveNext())
                {
                    al.Add(cacheEnumerator.Key);
                }
            }
            return al;
        }
    }
}